package com.guigu.health.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.health.dao.*;
import com.guigu.health.pojo.*;
import com.guigu.health.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName:RoleServicerImpl
 * @Description:角色业务实现
 * @Author:longtao
 * @Date:2021/4/19
 * @Version:1.0
 */

@Service(value = "roleService")
@SuppressWarnings("all")
public class RoleServiceImpl implements RoleService {

    private RoleMapper roleMapper;

    @Autowired
    @Qualifier(value = "roleMapper")
    public void setRoleMapper(RoleMapper roleMapper) {
        this.roleMapper = roleMapper;
    }

    private UserRoleMapper userRoleMapper;


    private UserRolePermissionMapper userRolePermissionMapper;
    @Autowired
    @Qualifier(value = "userRolePermissionMapper")
    public void setUserRolePermissionMapper(UserRolePermissionMapper userRolePermissionMapper) {
        this.userRolePermissionMapper = userRolePermissionMapper;
    }

    private UserMapper userMapper;
    @Autowired
    @Qualifier(value = "userMapper")
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    /**
     * 模糊查询角色并分页
     *
     * @param name
     * @return
     */
    @Override
    public PageInfo<RoleUsers> queryRoleByName(String name, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<RoleUsers> list = userRolePermissionMapper.queryByName(name);
        PageInfo<RoleUsers> pageInfo=new PageInfo<RoleUsers>(list);
        return pageInfo;
    }

    /**
     * 查询用户和角色之间的中间表中的角色ID
     *
     * @param userID
     * @return
     */
    @Override
    public List<Integer> getCountID(Integer userID) {
        List<Integer> countID = userRolePermissionMapper.getCountID(userID);
        return countID;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Transactional
    @Override
    public Boolean deleteRole(Integer id) {
        boolean flag = false;
        List<Integer> countID = userRolePermissionMapper.getCountID(id);
        /**
         * 删除角色和用户之间的关系
         */
        boolean b = deleteRole_UserKey(id);
        int i = roleMapper.deleteByPrimaryKey(id);
        if (i>0){
            flag=true;
        }
        return flag&&b;
    }

    /**
     * 修改
     *
     * @param role
     * @param example
     * @param user_roleKeys
     * @return
     */
    @Override
    @Transactional
    public Boolean updateRole(Role role, RoleExample example, List<User_RoleKey> user_roleKeys) {
        boolean flag = false;
        int i = roleMapper.updateByExampleSelective(role,example);
        boolean b = deleteRole_UserKey(role.getId());
        if (user_roleKeys.size()>0){
            int i1 = userRolePermissionMapper.batchAdd(user_roleKeys);
            if (i>0&&i1==user_roleKeys.size()){
                flag=true;
            }
        }else {
            if (i>0){
                flag=true;
            }
        }
        return flag;
    }

    /**
     * 添加
     *
     * @param role
     * @param user_roleKeys
     * @return
     */

    @Override
    @Transactional
    public boolean addRole(Role role, Integer [] user_roleKeys) {
        boolean flag = false;
      roleMapper.insert(role);
        Integer id = role.getId();
        if (user_roleKeys.length> 0 &&null !=user_roleKeys) {
            List<User_RoleKey> user_roleKeys1=new ArrayList<User_RoleKey>();
            for (Integer key : user_roleKeys) {
                User_RoleKey userRoleKey=new User_RoleKey();
                userRoleKey.setRole_id(id);
                userRoleKey.setUser_id(key);
                user_roleKeys1.add(userRoleKey);
            }
            int i = userRolePermissionMapper.batchAdd(user_roleKeys1);
            if (i == user_roleKeys.length) {
                flag = true;
            }
        }
        return flag;
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */

    @Override
    @Transactional
    public boolean deleteRole_UserKey(Integer id) {
        boolean flag = false;
        Integer delete = userRolePermissionMapper.delete(id);
        if (delete > 0) {
            flag = true;
        }
        return flag;
    }

    @Override
    public List<User> queryAllUser() {
        List<User> list = userMapper.selectByExample(null);
        return list;
    }

    /**
     * 查询所有角色
     *
     * @return
     */
    @Override
    public List<Role> queryAllRoles() {
        return roleMapper.selectByExample(null);
    }
}
